package com.upb.webserver.overview;

import com.upb.webserver.account.AccountService;
import com.upb.webserver.common.enums.RoleTypeEnum;
import com.upb.webserver.common.pojo.account.AccountCommon;
import com.upb.webserver.common.pojo.base.BasePageResponse;
import com.upb.webserver.common.pojo.base.BaseResponse;
import com.upb.webserver.common.pojo.overview.RespSysOverview;
import com.upb.webserver.common.returncode.ConstantCode;
import com.upb.webserver.rest.fisco.FiscoChainMgrRestService;
import com.upb.webserver.rest.fisco.FiscoDataFetchRestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


@Slf4j
@RestController
@RequestMapping(value = "system")
public class SystemOverviewController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private FiscoDataFetchRestService fiscoDataFetchRestService;
    @Autowired
    private FiscoChainMgrRestService fiscoChainMgrRestService;

    static boolean checkRole(AccountCommon accountCommon) {
        return (accountCommon.getRoleName().equalsIgnoreCase(RoleTypeEnum.DEVOPS_ADMIN.getName()) ||
                accountCommon.getRoleName().equalsIgnoreCase(RoleTypeEnum.DEVOPS_GENERAL.getName()));
    }

    @GetMapping(value = "/node/allCount")
    public BaseResponse getNodeAllCount(HttpServletRequest request) {
        log.info("start exec method [SystemOverviewController.getNodeAllCount].");
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        if (checkRole(accountCommon)) {
            return fiscoChainMgrRestService.getAllNodeSystem();
        }
        return new BaseResponse(ConstantCode.ACCESS_DENIED);
    }

    @PostMapping(value = "/tranx/allCount")
    public BaseResponse getTranxAllCount(@RequestBody @Valid RespSysOverview respSysOverview, HttpServletRequest request) {
        log.info("start exec method [SystemOverviewController.getTranxAllCount].");
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        if (checkRole(accountCommon)) {
            return fiscoDataFetchRestService.getTranxAllCount(respSysOverview);
        }
        return new BaseResponse(ConstantCode.ACCESS_DENIED);
    }

    @PostMapping(value = "/block/allCount")
    public BaseResponse getBlockAllCount(@RequestBody @Valid RespSysOverview respSysOverview, HttpServletRequest request) {
        log.info("start exec method [SystemOverviewController.getBlockAllCount].");
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        if (checkRole(accountCommon)) {
            return fiscoDataFetchRestService.getBlockAllCount(respSysOverview);
        }
        return new BaseResponse(ConstantCode.ACCESS_DENIED);
    }

    @PostMapping(value = "/tranx/allList")
    public BasePageResponse getTranxAllList(@RequestBody @Valid RespSysOverview respSysOverview, HttpServletRequest request) {
        log.info("start exec method [SystemOverviewController.getNodeAllCount].");
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        if (checkRole(accountCommon)) {
            return fiscoDataFetchRestService.getTranxAllList(respSysOverview);
        }
        return new BasePageResponse(ConstantCode.ACCESS_DENIED);
    }
}
